package cn.team11.smarteye.impl;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;

import com.bumptech.glide.Glide;

import cn.team11.sds.dayi.R;
import cn.team11.smarteye.contract.InComingCallContract;
import cn.team11.smarteye.service.CatEyeService;

/**
 * Created by Yang on 2017/11/7.
 */

public class InComingCallPresenterImpl implements InComingCallContract.Presenter{

  private InComingCallContract.View mView;
  private MediaPlayer mMediaPlayer;


  public InComingCallPresenterImpl(InComingCallContract.View mView) {
    this.mView = mView;
    mView.setPresenter(this);
  }


  @Override
  public void start() {

  }

  @Override
  public void destroy() {

  }

  @Override
  public void loadDoorPreviewImage(String fid, String bid) {

    if (CatEyeService.icvss != null){
      String imgUrl = CatEyeService.icvss.equesGetRingPictureUrl(fid, bid).toString();
      Glide.with(mView.getContext()).load(imgUrl).placeholder(R.drawable.ic_listen_logo).dontAnimate().into(mView.getPreviewImageView());
    }
  }


  @Override
  public void playRing() {
    try {
      Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setDataSource(mView.getContext(), alert);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
      mMediaPlayer.setLooping(true);
      mMediaPlayer.prepare();
      mMediaPlayer.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public void stopRing() {
    if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
      mMediaPlayer.stop();
    }
  }
}
